package com.hxh.generater.springBoot.simple.gen;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.generater.springBoot.genBase.GenMenu;
import com.hxh.util.GenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.*;

@Slf4j
public class GenSimpleMenu implements GenMenu {
    /**
     * @param project
     */
    @Override
    public void generationMenu(Project project) {
        try {
            generationTreeBuilder(project);
            generationMenuCache(project);
            generationRoleMenuCache(project);
        } catch (Exception e) {
            log.error(project.getTemplate() + "模板：初始化菜单相关异常！", e);
        }
    }

    private static void generationTreeBuilder(Project project) throws Exception {
        File classFile = GenUtil.newFile(project, "cache", "TreeBuilder");
        try {
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\cache\\TreeBuilder");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void generationMenuCache(Project project) throws Exception {
        File classFile = GenUtil.newFile(project, "cache", "MenuCache");
        try {
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\cache\\MenuCache");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void generationRoleMenuCache(Project project) throws Exception {
        File classFile = GenUtil.newFile(project, "cache", "RoleMenuCache");
        try {
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\cache\\RoleMenuCache");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
